﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Web;
using BbsbWebService.Service;
using BbsbWebService.Enums;
using BbsbWebService.Classes;

namespace BbsbWebService.Repositories
{
    public class ClientGroupRepository : GroupRepositoryBase
    {
        #region Private Members

        private IBbsbService _service;

        private IBbsbService Service
        {
            get
            {
                if (_service == null)
                {
                    WebChannelFactory<IBbsbService> factory = new WebChannelFactory<IBbsbService>(new Uri(AppSettings.NetworkServerAddress));
                    _service = factory.CreateChannel();
                }

                return _service;
            }
        }

        #endregion

        public override void Initialise()
        {
            var groups = Service.GetGroups(GroupType.All);
            foreach (var group in groups)
            {
                Groups.Add(new Group
                {
                    Id = group.Id,
                    Name = group.Name
                });
            }
        }

        public override void SaveGroup(BbsbWebService.Classes.Group group)
        {
            Service.SaveGroup(group);
        }

        public override void RemoveGroup(BbsbWebService.Classes.Group group)
        {
            Service.RemoveGroup(group);
        }
    }
}
